# 
# Steven W. Webster
# "Anger and Declining Trust in Government in the American Electorate"
# Experimental analysis replication code
#

setwd("")

library(dplyr)
library(car)
library(stargazer)
library(xtable)
library(ggplot2)
library(reshape2)

anger <- read.csv("aap.csv", header = TRUE)
anger <- anger[-1,]

# Recoding variable structure
# DVs
anger$cand_strong_position <- as.numeric(as.character(anger$cand_strong_position))
anger$compromise <- as.numeric(as.character(anger$compromise))
anger$blame_other_party <- as.numeric(as.character(anger$blame_other_party))
anger$govt_corrupt <- as.numeric(as.character(anger$govt_corrupt))
anger$govt_unresponsive <- as.numeric(as.character(anger$govt_unresponsive))
anger$govt_satisfied <- as.numeric(as.character(anger$govt_satisfied))
anger$vote_likelihood_2016 <- as.numeric(as.character(anger$vote_likelihood_2016))
anger$talk_about_2016 <- as.numeric(as.character(anger$talk_about_2016))
anger$donate_likelihood16 <- as.numeric(as.character(anger$donate_likelihood16))
anger$display_yard_sign16 <- as.numeric(as.character(anger$display_yard_sign16))
anger$work_campaign16 <- as.numeric(as.character(anger$work_campaign16))
anger$attend_rally16 <- as.numeric(as.character(anger$attend_rally16))

# Others
anger$ideology <- as.numeric(as.character(anger$ideology))
anger$ideology[anger$ideology==-99] <- NA

anger$income <- as.numeric(as.character(anger$income))
anger$income[anger$income==-99] <- NA

anger$pid7 <- as.numeric(as.character(anger$pid7))
anger$pid7[anger$pid7==-99] <- NA

anger$educ <- as.numeric(as.character(anger$educ))
anger$educ[anger$educ==-99] <- NA

anger$race_eth <- as.numeric(as.character(anger$race_eth))
anger$race_eth[anger$race_eth==-99] <- NA
anger <- anger %>% mutate(nonwhite = as.numeric(race_eth != 1))

anger$gender <- as.numeric(as.character(anger$gender))
anger$gender[anger$gender==-99] <- NA
anger <- anger %>% mutate(female = as.numeric(gender == 2))

anger$abortion <- as.numeric(as.character(anger$abortion))
anger$abortion[anger$abortion==-99] <- NA

anger$birth_citizenship <- as.numeric(as.character(anger$birth_citizenship))
anger$birth_citizenship[anger$birth_citizenship==-99] <- NA

anger$gay_marriage <- as.numeric(as.character(anger$gay_marriage))
anger$gay_marriage[anger$gay_marriage==-99] <- NA

anger$affirmative_action <- as.numeric(as.character(anger$affirmative_action))
anger$affirmative_action[anger$affirmative_action==-99] <- NA

anger$guns <- as.numeric(as.character(anger$guns))
anger$guns[anger$guns==-99] <- NA

anger$global_warming <- as.numeric(as.character(anger$global_warming))
anger$global_warming[anger$global_warming==-99] <- NA

anger$vote <- as.numeric(as.character(anger$vote))
anger$vote[anger$vote==-99] <- NA
anger <- anger %>% mutate(vote2016 = as.numeric(vote == 1))

anger$talk_politics <- as.numeric(as.character(anger$talk_politics))
anger$talk_politics[anger$talk_politics==-99] <- NA
anger$talk_politics <- recode(anger$talk_politics, "1=0;2=1;3=2;4=3;5=4;6=5;7=6;8=7")

anger$display_sign <- as.numeric(as.character(anger$display_sign))
anger$display_sign[anger$display_sign==-99] <- NA
anger <- anger %>% mutate(display_sign16 = as.numeric(display_sign == 1))

anger$persuade_vote <- as.numeric(as.character(anger$persuade_vote))
anger$persuade_vote[anger$persuade_vote==-99] <- NA
anger <- anger %>% mutate(persuade_vote_choice = as.numeric(persuade_vote == 1))

anger$donate <- as.numeric(as.character(anger$donate))
anger$donate[anger$donate==-99] <- NA
anger <- anger %>% mutate(donate_money = as.numeric(donate == 1))

anger$written_politician <- as.numeric(as.character(anger$written_politician))
anger$written_politician[anger$written_politician==-99] <- NA

anger$vote_2012 <- as.numeric(as.character(anger$vote_2012))
anger$vote_2012[anger$vote_2012==-99] <- NA
anger <- anger %>% mutate(vote2012 = as.numeric(vote_2012 == 1))

na.omit(anger)

# Participation scale
anger <- anger %>% mutate(participation_scale = vote2016 + display_sign16 + persuade_vote_choice +
                            donate_money + written_politician + talk_politics + vote2012)

# Fix missing values on dependent variables
anger$cand_strong_position[anger$cand_strong_position==-99] <- NA
anger$compromise[anger$compromise==-99] <- NA
anger$blame_other_party[anger$blame_other_party==-99] <- NA
anger$govt_corrupt[anger$govt_corrupt==-99] <- NA
anger$govt_unresponsive[anger$govt_unresponsive==-99] <- NA
anger$govt_satisfied[anger$govt_satisfied==-99] <- NA
anger$vote_likelihood_2016[anger$vote_likelihood_2016==-99] <- NA
anger$talk_about_2016[anger$talk_about_2016==-99] <- NA
anger$donate_likelihood16[anger$donate_likelihood16==-99] <- NA
anger$display_yard_sign16[anger$display_yard_sign16==-99] <- NA
anger$work_campaign16[anger$work_campaign16==-99] <- NA
anger$attend_rally16[anger$attend_rally16==-99] <- NA
na.omit(anger)

# Descriptives
sstats <- anger %>% select(educ, race_eth, gender, pid7, ideology)
sstats2 <- sstats %>%
  mutate(White = ifelse(race_eth == 1, 1, 0),
         Black = ifelse(race_eth == 2, 1, 0),
         Asian = ifelse(race_eth == 3, 1, 0),
         NativeAmerican = ifelse(race_eth == 4, 1, 0),
         Hispanic = ifelse(race_eth == 5, 1, 0),
         OtherRace = ifelse(race_eth == 6, 1, 0),
         HighSchoolOnly = ifelse(educ == 1, 1, 0),
         SomeCollege = ifelse(educ >= 2, 1, 0),
         Male = ifelse(gender == 1, 1, 0),
         Female = ifelse(gender == 2, 1, 0),
         Democrat = ifelse(pid7 < 4, 1, 0),
         Independent = ifelse(pid7 == 4, 1, 0),
         Republican = ifelse(pid7 > 4, 1, 0),
         Liberal = ifelse(ideology < 4, 1, 0),
         Conservative = ifelse(ideology > 4, 1, 0)) %>%
  select(White, Black, Asian, NativeAmerican, Hispanic, OtherRace,
         HighSchoolOnly, SomeCollege, Male, Female, Democrat, Independent,
         Republican, Liberal, Conservative)

stargazer(sstats2,
          type="latex",
          style="apsr",
          title="Summary Statistics of Personality Data",
          #notes="This table displays summary statistics for the data used in the analyses in this paper.",
          #font.size="footnotesize",
          label="tab:summary_stats",
          out="")

# Create indicators for treatment status
anger <- anger %>% mutate(anger_treated = as.numeric(anger != ""),
                          anger_salience_treated = as.numeric(anger_politics != ""),
                          salience_treated = as.numeric(salience != ""),
                          control = as.numeric(breakfast_control != ""))

# Party ID dummies
anger <- anger %>% mutate(republican = as.numeric(pid7 > 4),
                          democrat = as.numeric(pid7 < 4))

# Models for Table 3
experiment_unconditional <- lm(govt_unresponsive ~ anger_treated + anger_salience_treated + salience_treated, data = anger)
experiment_controls <- lm(govt_unresponsive ~ anger_treated + anger_salience_treated + salience_treated + pid7 + female + nonwhite + income + ideology + educ, data = anger)

